//
//  School.m
//  test
//
//  Created by yun on 2018/4/4.
//  Copyright © 2018年 yun. All rights reserved.
//

#import "School.h"
#import "University.h"

@implementation School

/*
 触发机制
 1、resolveInstanceMethod和resolveClassMethod 返回NO ---> forwardingTargetForSelector 返回nil或self --->forwardInvocation
 */

#pragma mark -- 重定向
/*
 forwardingTargetForSelector 返回nil或self ---> forwardInvocation:(消息转发)
 消息对象更换
 */
//- (id)forwardingTargetForSelector:(SEL)aSelector{
//    if (aSelector == @selector(goSchool)) {
//        return [University new];// 对象
//    }
//    return [super forwardingTargetForSelector:aSelector];
//}
//
//+ (id)forwardingTargetForSelector:(SEL)aSelector{
//    if (aSelector == @selector(goH)) {
////        return NSClassFromString(@"University");
//        return [University class];// 类对象
//    }
//    return [super forwardingTargetForSelector:aSelector];
//}

#pragma mark -- 转发
// 先触发
- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector{
    
    return [[University new] methodSignatureForSelector:aSelector];
}
// 再触发
- (void)forwardInvocation:(NSInvocation *)anInvocation{
    if (anInvocation.selector == @selector(goSchool)) {
        // target发送消息
        [anInvocation invokeWithTarget:[University new]];
    } else {
        [super forwardInvocation:anInvocation];
    }
 
}



+ (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector{
    
    return [[University class] methodSignatureForSelector:aSelector];
}

+ (void)forwardInvocation:(NSInvocation *)anInvocation{
    if (anInvocation.selector == @selector(goH)) {
        [anInvocation invokeWithTarget:[University class]];
    } else {
        [super forwardInvocation:anInvocation];
    }
    
}





@end
